//
//  GridCell.m
//  iphoneMobo
//
//  Created by Mac Book-1 on 10-7-19.
//  Copyright 2010 shijiazheng. All rights reserved.
//

#import "GridCell.h"
#import "GridItem.h"
#import "ImageFactory.h"


@implementation GridCell

@synthesize iColumnsArray;


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
		iColumnsArray = [[NSMutableArray alloc] init];
        self.accessoryType = UITableViewCellAccessoryNone;
		self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];

    // Configure the view for the selected state

}

- (void)dealloc {
	[iColumnsArray release];
    [super dealloc];
}

// called when the checkmark button is touched 
- (void)checkAction:(id)sender
{
    int index = [sender tag];
	GridItem* item = [iColumnsArray objectAtIndex:index];
	//取链接地址
	if (item.iUrl) {
		NSMutableDictionary *aUserInfo = [[NSMutableDictionary alloc] init];
		[aUserInfo setObject:item.iUrl forKey:@"url"];
		NSNumber *aMethod = [NSNumber numberWithInt:1];//POST
		NSNumber *aFlag = [NSNumber numberWithInt:0];
		[aUserInfo setObject:aMethod forKey:@"method"];
		[aUserInfo setObject:aFlag forKey:@"flag"];
		//[[NSNotificationCenter defaultCenter] postNotificationName:CustomSendRequestMessageNotification object:Nil userInfo:aUserInfo];
		[aUserInfo release];
	}
}

- (void) AddColumnData:(id) aColumn
{
	CGRect rect = [(GridItem*)aColumn frame];
	UIView* subview = [[UIView alloc] initWithFrame:rect];
	rect.origin.x = 0;
	UIButton *imgButton = [[UIButton alloc] initWithFrame:rect];
	imgButton.tag = iColumnsArray.count;
	imgButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
	imgButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
	[imgButton addTarget:self action:@selector(checkAction:) forControlEvents:UIControlEventTouchDown];
	UIImage* normalImg = [[CImageFactory getInstance] getUIImage:[(GridItem*)aColumn iNomalImg]];
	[imgButton setImage:normalImg forState:UIControlStateNormal];
	[normalImg release];
	if ([[(GridItem*)aColumn iNomalImg] caseInsensitiveCompare:[(GridItem*)aColumn iFocusImg]] != 0) {
		UIImage* focusImg = [[CImageFactory getInstance] getUIImage:[(GridItem*)aColumn iFocusImg]];
		[imgButton setImage:focusImg forState:UIControlStateSelected];
		[focusImg release];
	}
	[subview addSubview:imgButton];
	[imgButton release];
	rect = subview.bounds;
	rect.origin.x = 0;
	rect.origin.y = subview.bounds.origin.y + subview.bounds.size.height - 18;
	rect.size.height = 18;
	UILabel* label = [[UILabel alloc] initWithFrame:rect];
	[label setBackgroundColor:[UIColor clearColor]];
	label.textAlignment = UITextAlignmentCenter;
	label.adjustsFontSizeToFitWidth = YES;
	[label setText:[(GridItem*)aColumn iText]];
	[subview addSubview:label];
	[label release];
	[self.contentView addSubview:subview];
	[subview release];
	[iColumnsArray addObject:aColumn];
	//[aColumn release];
}

@end
